<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="烟草站" prop="levelTobaccoStation">
        <el-input
          v-model="queryParams.levelTobaccoStation"
          placeholder="请输入烟草站"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="村庄" prop="levelVillage">
        <el-input
          v-model="queryParams.levelVillage"
          placeholder="请输入村庄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="相关人员" prop="levelName">
        <el-input
          v-model="queryParams.levelName"
          placeholder="请输入相关人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['nitrogen:level:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nitrogen:level:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nitrogen:level:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['nitrogen:level:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="levelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="烟草站" align="center" prop="levelTobaccoStation" />
      <el-table-column label="村庄" align="center" prop="levelVillage" />
      <el-table-column label="相关人员" align="center" prop="levelName" />
      <el-table-column label="种植面积" align="center" prop="levelArea" />
      <el-table-column label="复合肥数" align="center" prop="levelFertilizerDistributionCompoundFertilizer" />
      <el-table-column label="提苗肥数" align="center" prop="levelFertilizerDistributionSeedlingFertilizer" />
      <el-table-column label="有机肥数" align="center" prop="levelFertilizerDistributionOrganicFertilizer" />
      <el-table-column label="花生麸数" align="center" prop="levelFertilizerDistributionPeanutBran" />
      <el-table-column label="其他肥料数量" align="center" prop="levelOtherFertilizer" />
      <el-table-column label="总施氮" align="center" prop="levelTotalNitrogenApplication" />
      <el-table-column label="每亩纯氮施用量" align="center" prop="levelPerMuPureNitrogenApplication" />
      <el-table-column label="备注" align="center" prop="levelRemarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['nitrogen:level:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['nitrogen:level:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改施氮水平对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="烟草站" prop="levelTobaccoStation">
          <el-input v-model="form.levelTobaccoStation" placeholder="请输入烟草站" />
        </el-form-item>
        <el-form-item label="村庄" prop="levelVillage">
          <el-input v-model="form.levelVillage" placeholder="请输入村庄" />
        </el-form-item>
        <el-form-item label="相关人员" prop="levelName">
          <el-input v-model="form.levelName" placeholder="请输入相关人员" />
        </el-form-item>
        <el-form-item label="种植面积" prop="levelArea">
          <el-input v-model="form.levelArea" placeholder="请输入种植面积" />
        </el-form-item>
        <el-form-item label="复合肥数" prop="levelFertilizerDistributionCompoundFertilizer">
          <el-input v-model="form.levelFertilizerDistributionCompoundFertilizer" placeholder="请输入复合肥数" />
        </el-form-item>
        <el-form-item label="提苗肥数" prop="levelFertilizerDistributionSeedlingFertilizer">
          <el-input v-model="form.levelFertilizerDistributionSeedlingFertilizer" placeholder="请输入提苗肥数" />
        </el-form-item>
        <el-form-item label="有机肥数" prop="levelFertilizerDistributionOrganicFertilizer">
          <el-input v-model="form.levelFertilizerDistributionOrganicFertilizer" placeholder="请输入有机肥数" />
        </el-form-item>
        <el-form-item label="花生麸数" prop="levelFertilizerDistributionPeanutBran">
          <el-input v-model="form.levelFertilizerDistributionPeanutBran" placeholder="请输入花生麸数" />
        </el-form-item>
        <el-form-item label="其他肥料数量" prop="levelOtherFertilizer">
          <el-input v-model="form.levelOtherFertilizer" placeholder="请输入其他肥料数量" />
        </el-form-item>
        <el-form-item label="总施氮" prop="levelTotalNitrogenApplication">
          <el-input v-model="form.levelTotalNitrogenApplication" placeholder="请输入总施氮" />
        </el-form-item>
        <el-form-item label="每亩纯氮施用量" prop="levelPerMuPureNitrogenApplication">
          <el-input v-model="form.levelPerMuPureNitrogenApplication" placeholder="请输入每亩纯氮施用量" />
        </el-form-item>
        <el-form-item label="备注" prop="levelRemarks">
          <el-input v-model="form.levelRemarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLevel, getLevel, delLevel, addLevel, updateLevel } from "@/api/nitrogen/level";

export default {
  name: "Level",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 施氮水平表格数据
      levelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        levelTobaccoStation: null,
        levelVillage: null,
        levelName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        levelTobaccoStation: [
          { required: true, message: "烟草站不能为空", trigger: "blur" }
        ],
        levelVillage: [
          { required: true, message: "村庄不能为空", trigger: "blur" }
        ],
        levelName: [
          { required: true, message: "相关人员不能为空", trigger: "blur" }
        ],
        levelArea: [
          { required: true, message: "种植面积不能为空", trigger: "blur" }
        ],
        levelFertilizerDistributionCompoundFertilizer: [
          { required: true, message: "复合肥数不能为空", trigger: "blur" }
        ],
        levelFertilizerDistributionSeedlingFertilizer: [
          { required: true, message: "提苗肥数不能为空", trigger: "blur" }
        ],
        levelFertilizerDistributionOrganicFertilizer: [
          { required: true, message: "有机肥数不能为空", trigger: "blur" }
        ],
        levelFertilizerDistributionPeanutBran: [
          { required: true, message: "花生麸数不能为空", trigger: "blur" }
        ],
        levelOtherFertilizer: [
          { required: true, message: "其他肥料数量不能为空", trigger: "blur" }
        ],
        levelTotalNitrogenApplication: [
          { required: true, message: "总施氮不能为空", trigger: "blur" }
        ],
        levelPerMuPureNitrogenApplication: [
          { required: true, message: "每亩纯氮施用量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询施氮水平列表 */
    getList() {
      this.loading = true;
      listLevel(this.queryParams).then(response => {
        this.levelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        levelTobaccoStation: null,
        levelVillage: null,
        levelName: null,
        levelArea: null,
        levelFertilizerDistributionCompoundFertilizer: null,
        levelFertilizerDistributionSeedlingFertilizer: null,
        levelFertilizerDistributionOrganicFertilizer: null,
        levelFertilizerDistributionPeanutBran: null,
        levelOtherFertilizer: null,
        levelTotalNitrogenApplication: null,
        levelPerMuPureNitrogenApplication: null,
        levelRemarks: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加施氮水平";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLevel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改施氮水平";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLevel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLevel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除施氮水平编号为"' + ids + '"的数据项？').then(function() {
        return delLevel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('nitrogen/level/export', {
        ...this.queryParams
      }, `level_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
